package com.codegravity.mobile.expensms;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class ExpenseBarChart extends View {

	static String days[] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
	static float width, height;
	static float bar_width = 0, bar_gap = 0;
	static float chart_height = 0, chart_width = 0;
	static float chart_left = 0, chart_top = 0, chart_right = 0,
			chart_bottom = 0;
	static float xAxis = 0, yAxis = 0, axis_gap = 0;
	String week;
	float max_expense = 0, avg_weekly = 0, avg_mnth = 0;

	List<Float> expense_amounts, expense_yval;
	List<Integer> expensesCnt_lst;
	static Paint bar_clr = new Paint();
	static Paint bar_clr1 = new Paint();
	static TextPaint txt_clr = new TextPaint();
	static Paint num_clr = new Paint();
	DecimalFormat df = new DecimalFormat("#.##");
	static int isSun = 0;

	public ExpenseBarChart(Context context, List<Float> exp_amnt,
			List<Integer> expCnt_lst, String week, float avg_w, float avg_m,
			int frm_widget, int isSun) {
		super(context);
		ExpenseBarChart.isSun = isSun;
		this.avg_mnth = avg_m;
		this.avg_weekly = avg_w;
		this.week = week;
		expensesCnt_lst = expCnt_lst;
		expense_amounts = exp_amnt;
		expense_yval = new ArrayList<Float>();

		Display display = ((WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		width = display.getWidth();
		height = display.getHeight();
		if (frm_widget == 1) {
			height = (int) (height - (height * 0.20));
			width = (int) (width - (width * 0.05));
			setBackgroundColor(Color.TRANSPARENT);
			setBackgroundResource(R.drawable.bkg_widget);
		} else
			setBackgroundResource(R.drawable.background);

		chart_left = (int) (width * 0.05);
		chart_width = (width - ((2 * chart_left)));
		setColours();

		chart_top = 5 * getTextSize();
		chart_right = width - chart_left;
		chart_bottom = height - chart_top;

		axis_gap = (int) (height * 0.05);

		chart_height = (height - ((2 * chart_top) + (2 * axis_gap) + getTextSize()));

		bar_width = chart_width / 11;
		bar_gap = bar_width / 2;
		max_expense = Collections.max(expense_amounts);
		setYaxis_values();
	}

	private void setColours() {
		bar_clr.setColor(Color.YELLOW);
		bar_clr1.setColor(Color.YELLOW);
		// bar_clr.setStyle(Style.STROKE);

		txt_clr.setColor(Color.BLACK);
		txt_clr.setTextSize(getResources().getDimensionPixelSize(
				R.dimen.font_size));
		txt_clr.setTypeface(Typeface.DEFAULT_BOLD);
		txt_clr.setAntiAlias(true);
		while ((txt_clr.measureText("Wed") * 10) > chart_width)
			txt_clr.setTextSize(txt_clr.getTextSize() - 1);
		bar_width = txt_clr.measureText("Wed");
		bar_gap = bar_width / 2;
		num_clr.setColor(Color.WHITE);
		num_clr.setTextSize(getResources().getDimensionPixelSize(
				R.dimen.font_size));
		num_clr.setAntiAlias(true);

	}

	private void setYaxis_values() {
		for (float val : expense_amounts) {
			if (max_expense != 0)
				expense_yval.add((val * chart_height) / max_expense);
			else
				expense_yval.add((float) 0.0);
		}
	}

	float currBarX = 0;

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		currBarX = chart_left + bar_gap;
		/*
		 * canvas.drawLine(chart_left, chart_top, chart_left, chart_bottom,
		 * bar_clr);
		 */
		canvas.drawLine(chart_left, chart_bottom - axis_gap, chart_right,
				chart_bottom - axis_gap, txt_clr);
		for (int i = 0; i < expense_amounts.size(); i++) {
			bar_clr1.setShader(new LinearGradient(currBarX, chart_bottom
					- axis_gap, currBarX + bar_width / 2, chart_bottom
					- axis_gap, Color.rgb(102, 102, 251), Color.rgb(142, 142,
					251), Shader.TileMode.CLAMP));
			canvas.drawRect(currBarX, chart_bottom - axis_gap, currBarX
					+ bar_width / 2,
					chart_bottom - axis_gap - expense_yval.get(i), bar_clr1);
			bar_clr.setShader(new LinearGradient(currBarX + bar_width / 2,
					chart_bottom - axis_gap, currBarX + bar_width, chart_bottom
							- axis_gap, Color.rgb(142, 142, 251), Color.rgb(
							160, 160, 253), Shader.TileMode.CLAMP));

			canvas.drawRect(currBarX + bar_width / 2, chart_bottom - axis_gap,
					currBarX + bar_width, chart_bottom - axis_gap
							- expense_yval.get(i), bar_clr);

			canvas.drawText(days[(i + isSun) % 7], currBarX, chart_bottom
					- axis_gap + getTextSize() + 5, txt_clr);
			canvas.drawText("" + df.format(expense_amounts.get(i)), currBarX,
					chart_bottom - axis_gap - expense_yval.get(i)
							- getTextSize(), txt_clr);

			canvas.drawText("" + expensesCnt_lst.get(i), currBarX, chart_top,
					txt_clr);
			currBarX = currBarX + bar_gap + bar_width;
		}
		canvas.drawText(week, width / 2 - getTextLen(week) / 2, height - 10,
				txt_clr);
		String text = "Average Weekly: " + df.format(avg_weekly);
		canvas.drawText(text, width / 2 - getTextLen(text) / 2, getTextSize(),
				txt_clr);
		text = "Average Monthly: " + df.format(avg_mnth);
		canvas.drawText(text, width / 2 - getTextLen(text) / 2,
				2 * getTextSize() + 5, txt_clr);

	}

	float getTextLen(String str) {
		return txt_clr.measureText(str);
	}

	float getTextSize() {
		return -(txt_clr.ascent() + txt_clr.descent());
	}
}